/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.solaris;

import android.widget.Toast;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Frank
 */
public class Connection {
    
    private static Connection connection = null;
    private HttpClient httpclient;
    
    private String lokaalIP = "10.0.2.2";
    
    protected Connection() {
        this.makeConnection();
    }
    
    public static Connection instantiate() {
        if(connection == null) {
            connection = new Connection();
        }
        return connection;
    }
    
    private void makeConnection() {
        this.httpclient = new DefaultHttpClient();  
    }
    
    private String doPost(String url, List<NameValuePair> postArray) {
        
        try {
            
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(postArray));
            String response = httpclient.execute(httppost, new BasicResponseHandler());
        
            return response;
        
        } catch(ClientProtocolException e) {
            System.out.println(e);
        } catch(IOException e) {
            System.out.println(e);
        }
        
        return "NoResponse";
        
    }
    
    public boolean checkLogin(String username, String password) {
        
        // Waarden voor de post in array stoppen
        List<NameValuePair> postArray = new ArrayList<NameValuePair>(2);
        postArray.add(new BasicNameValuePair("username", username));
        postArray.add(new BasicNameValuePair("password", password));
        
        // Url 
        String url = "http://"+lokaalIP+"/server.solaris/checklogin.php";
        
        // Post uitvoeren
        String response = this.doPost(url, postArray);
        
        // Response checken
        if(response.equals("LOGINOK")) {
            return true;
        } 
               
        return false;
        
    }
    
    public boolean saveObservation(String seeing, String transparency, String groupnorth, String spotnorth, String groupsouth, String spotsouth, String location, String telescoop, String correction) {
        
        Session session = Session.instantiate();
        String username = session.getUsername();
        String password = session.getPassword();
        
        // Waarden voor de post in array stoppen
        List<NameValuePair> postArray = new ArrayList<NameValuePair>(2);
        postArray.add(new BasicNameValuePair("username", username));
        postArray.add(new BasicNameValuePair("password", password));
        postArray.add(new BasicNameValuePair("seeing", seeing));
        postArray.add(new BasicNameValuePair("transparency", transparency));
        postArray.add(new BasicNameValuePair("groupnorth", groupnorth));
        postArray.add(new BasicNameValuePair("spotnorth", spotnorth));
        postArray.add(new BasicNameValuePair("groupsouth", groupsouth));
        postArray.add(new BasicNameValuePair("spotsouth", transparency));
        postArray.add(new BasicNameValuePair("location", location));
        postArray.add(new BasicNameValuePair("telescoop", telescoop));
        postArray.add(new BasicNameValuePair("correction", correction));
        
        // Url 
        String url = "http://"+lokaalIP+"/server.solaris/addobservation.php";
        
        // Post uitvoeren
        String response = this.doPost(url, postArray);
        
        // Response checken
        if(response.equals("OBSERVATIONADDED")) {            
            return true;
        } 
        
        return false;
        
    }
    
    public HashMap getTelescopen(String username, String password) {

        HashMap telescopen = new HashMap(); 
        
        // Waarden voor de post in array stoppen
        List<NameValuePair> postArray = new ArrayList<NameValuePair>(2);
        postArray.add(new BasicNameValuePair("username", username));
        postArray.add(new BasicNameValuePair("password", password));
        
        // Url 
        String url = "http://"+lokaalIP+"/server.solaris/telescoop.php";
        
        // Post uitvoeren
        String response = this.doPost(url, postArray);
        
        try {
            
            // We zetten response om naar JSON
            JSONObject json = new JSONObject(response);
            
            // Keys ophalen van json
            Iterator<Object> keys = json.keys();
           
            while(keys.hasNext()){
                
                // Waarden uit json lezen
                String key = (String)keys.next();
                String value = json.getString(key);

                // Wegschrijven naar hashmap
                telescopen.put(key, value);
                
            }
            
            
        } catch(JSONException e){
            System.out.println(e);
        }
        
        return telescopen;

    }
    
    public String MD5(String md5) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
        }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
            
        }
        return null;
    }
    
}
